﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.UI;
using System.Configuration;
namespace HFController
{
    public class BagePages :Page
    {
        /// <summary>
        /// 构造函数
        /// </summary>
        public void BagePage()
        {
            
        }

        /// <summary>
        /// 得到上传文件夹路径
        /// </summary>
        public string getUpFileFolder
        {
            get { return ConfigurationManager.AppSettings["UpfileFolder"].ToString().Trim(); }
        }

        /// <summary>
        /// 得到前台的地址
        /// </summary>
        public string getDemain
        {
            get { return ConfigurationManager.AppSettings["Domain"].ToString().Trim(); }
        }

        /// <summary>
        /// 得到后台的地址
        /// </summary>
        public string getManageDemain
        {
            get { return ConfigurationManager.AppSettings["manageDomain"].ToString().Trim(); }
        }

        /// <summary>
        /// 得到当前城市ID
        /// </summary>
        /// <returns></returns>
        protected int getNowCityID()
        {
            if (HttpContext.Current.Session["city"] == null)
            {
                return 1;
            }
            return int.Parse(HttpContext.Current.Session["city"].ToString());
        }

        /// <summary>
        /// 检测管理员是否登录
        /// </summary>
        protected void checkAdminIsLogin()
        {
            if (HttpContext.Current.Session["Admin"] == null)
            {
                Response.Redirect("/login.aspx");
            }
        }

        /// <summary>
        /// 得到管理员信息
        /// </summary>
        /// <returns></returns>
        protected hf.Model.admin getAdminInfo()
        {
            hf.Model.admin adminInfo = null;
            if (HttpContext.Current.Session["Admin"] != null)
            {
                adminInfo = (hf.Model.admin)HttpContext.Current.Session["Admin"];
            }
            return adminInfo;
        }

        /// <summary>
        /// 检测登录
        /// </summary>
        protected void checkAccountIsLogin()
        {
            if (this.getLoginAccount() == null)
            {
                Response.Redirect(this.getDemain + "/Default.aspx");
            }
        }

        protected void checkLawyerIsLogin()
        {
            if (this.getLoginLawyer() == null)
            {
                Response.Redirect(this.getDemain + "/Default.aspx");
            }
        }
        /*
        /// <summary>
        /// 检验SA权限
        /// </summary>
        protected void checkSA()
        {
            model.VO.Admin admin = AdminController.getController().getAdmin();
            if (admin.ID != 1)
            {
                Response.Write("您当前的级别无法访问此页！");
                Response.End();
            }
        }
        */
        /// <summary>
        /// 得到URL数据
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string GetQuery(string Key)
        {
            if (HttpContext.Current.Request.QueryString[Key] == null)
            {
                return string.Empty;
            }
            else {
                return HttpContext.Current.Request.QueryString[Key].ToString();
            }
        }

        /// <summary>
        /// 得到Post数据
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string GetForm(string Key)
        {
            if (HttpContext.Current.Request.Form[Key] == null)
            {
                return string.Empty;
            }
            else
            {
                return HttpContext.Current.Request.Form[Key].ToString();
            }
        }
        /// <summary>
        /// 得到Post和Get数据（通用）
        /// </summary>
        /// <param name="Key"></param>
        /// <returns></returns>
        public string GetAll(string Key)
        {
            if (HttpContext.Current.Request[Key] == null)
            {
                return string.Empty;
            }
            else
            {
                return HttpContext.Current.Request[Key].ToString();
            }
        }
        
        /// <summary>
        /// 检测用户是否登录
        /// </summary>
        public hf.Model.User getLoginAccount()
        {
            hf.Model.User account = null;
            if (HttpContext.Current.Session["User"] != null)
            {
                account = (hf.Model.User)HttpContext.Current.Session["User"];
            }
            return account;
        }

        /// <summary>
        /// 检测律师是否登录
        /// </summary>
        /// <returns></returns>
        public hf.Model.Lawyer getLoginLawyer()
        {
            hf.Model.Lawyer account = null;
            if (HttpContext.Current.Session["Lawyer"] != null)
            {
                account = (hf.Model.Lawyer)HttpContext.Current.Session["Lawyer"];
            }
            return account;
            
        }
        
        /// <summary>
        /// 时间差
        /// </summary>
        /// <param name="dt1">当前时间</param>
        /// <param name="dt2">要比较的时间</param>
        /// <returns></returns>
        public string TimeDifference(DateTime dt1, DateTime dt2)
        {
            TimeSpan sj1 = new TimeSpan(dt1.Ticks);
            TimeSpan sj2 = new TimeSpan(dt2.Ticks);
            TimeSpan sj = sj1.Subtract(sj2).Duration();
            if (sj.Days > 3)
            {
                return Convert.ToDateTime(dt2).ToString("M月d日");
            }
            else if (sj.Days != 0)
            {
                if (sj.Days == 1)
                {
                    return "昨天" + "&nbsp;" + Convert.ToDateTime(dt2).ToString("HH:mm");
                }
                else if (sj.Days == 2)
                {
                    return "前天" + "&nbsp;" + Convert.ToDateTime(dt2).ToString("HH:mm");
                }
                else
                {
                    return sj.Days.ToString() + "天前";
                }
            }
            else if (sj.Hours != 0)
            {
                //24小时之间
                if (Convert.ToInt32(dt1.Day) == Convert.ToInt32(dt2.Day))
                {
                    return "今天" + "&nbsp;" + Convert.ToDateTime(dt2).ToString("HH:mm");
                }
                else
                {
                    return "昨天" + "&nbsp;" + Convert.ToDateTime(dt2).ToString("HH:mm");
                }
            }
            else if (sj.Minutes != 0)
            {
                return sj.Minutes.ToString() + "分钟前";
            }
            else if (sj.Seconds < 20)
            {
                return "刚刚";
            }
            else
            {
                return sj.Seconds.ToString() + "秒前";
            }
        }

        /// <summary>
        /// 去掉HTML
        /// </summary>
        /// <param name="Htmlstring"></param>
        /// <returns></returns>
        public string DelHTML(string Htmlstring)//将HTML去除
        {
            #region
            //删除脚本

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<script[^>]*?>.*?</script>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            //删除HTML

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<(.[^>]*)>", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"([/r/n])[/s]+", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"-->", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"<!--.*", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            //Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<A>.*</A>","");

            //Htmlstring =System.Text.RegularExpressions. Regex.Replace(Htmlstring,@"<[a-zA-Z]*=/.[a-zA-Z]*/?[a-zA-Z]+=/d&/w=%[a-zA-Z]*|[A-Z0-9]","");



            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(quot|#34);", "/", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(amp|#38);", "&", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(lt|#60);", "<", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(gt|#62);", ">", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(nbsp|#160);", " ", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(iexcl|#161);", "/xa1", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(cent|#162);", "/xa2", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(pound|#163);", "/xa3", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&(copy|#169);", "/xa9", System.Text.RegularExpressions.RegexOptions.IgnoreCase);

            Htmlstring = System.Text.RegularExpressions.Regex.Replace(Htmlstring, @"&#(/d+);", "", System.Text.RegularExpressions.RegexOptions.IgnoreCase);


            Htmlstring.Replace("<", "");

            Htmlstring.Replace(">", "");

            Htmlstring.Replace("/r/n", "");

            //Htmlstring=HttpContext.Current.Server.HtmlEncode(Htmlstring).Trim();
            #endregion

            return Htmlstring;

        }

        /// <summary>
        /// 截取过长的字符串
        /// </summary>
        /// <param name="inputString"></param>
        /// <param name="len"></param>
        /// <param name="showtail"></param>
        /// <returns></returns>
        public string CutString(string inputString, int len, bool showtail)
        {

            ASCIIEncoding ascii = new ASCIIEncoding();
            int tempLen = 0;
            string tempString = "";
            if (inputString != "")
            {
                byte[] s = ascii.GetBytes(inputString);
                for (int i = 0; i < s.Length; i++)
                {
                    if ((int)s[i] == 63)
                    {
                        tempLen += 2;
                    }
                    else
                    {
                        tempLen += 1;
                    }

                    try
                    {
                        tempString += inputString.Substring(i, 1);
                    }
                    catch
                    {
                        break;
                    }

                    if (tempLen > len)
                        break;
                }
                //如果截过则加上半个省略号
                byte[] mybyte = System.Text.Encoding.Default.GetBytes(inputString);
                if (showtail == true)
                {
                    if (mybyte.Length > len)
                        tempString += "…";
                }
            }
            return tempString;
        }
        /// <summary>
        /// 得到城市名称
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public string getProvinceName(string ID)
        {
            return CityController.getController().getProvinceList().Find(i => i.ID == int.Parse(ID)).SName;
        }
        /// <summary>
        /// 得到城市名称
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public string getCityName(string ID)
        {
            return CityController.getController().getCityList().Find(i => i.ID == int.Parse(ID)).CityName;
        }
        /// <summary>
        /// 得到工作职位名称
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public string getJobsName(string ID)
        {
            return JobsController.getController().getJobsList(99999).Find(i => i.ID == int.Parse(ID)).JobsName;
        }
        /// <summary>
        /// 得到法律事务
        /// </summary>
        /// <param name="ID"></param>
        /// <returns></returns>
        public string getLegalAffairs(string ID) {
            return LegalAffairsController.getController().getLegalAffairsList(99999).Find(i => i.ID == int.Parse(ID)).LegalAffairsTitle;
        }
        /// <summary>
        /// 得到法律事务大类
        /// </summary>
        /// <param name="ID"></param>
        /// <param name="bigID"></param>
        /// <returns></returns>
        public string getLegalAffairsBig(string ID ,string bigID)
        {
            return LegalAffairsController.getController().getLegalAffairsBiglClassList(9999, int.Parse(bigID)).Find(i=>i.ID == int.Parse(ID)).ClassName;
        } 
 
        /// <summary>
        /// 得到指定省份的律师总数
        /// </summary>
        /// <param name="ProvinceID"></param>
        /// <returns></returns>
        public int LawyerCountForProvince(string ProvinceID)
        {
            return LawyerController.getController().getLawyerCount(int.Parse(ProvinceID)); ;
        }
        /// <summary>
        /// 得到问题状态
        /// </summary>
        /// <param name="StatusID"></param>
        /// <returns></returns>
        public string GetAskStatus(string StatusID)
        {
            string outstr = "";
            switch (StatusID)
            { 
                case "0":
                    outstr = "待解决";
                    break;
                case "1":
                    outstr = "已解决";
                    break;
                case "2":
                    outstr = "已关闭";
                    break;
            }
            return outstr;
        }

        /// <summary>
        /// 检测
        /// </summary>
        /// <param name="DelegateID"></param>
        /// <returns></returns>
        public bool checkShowDelegate(string DelegateID)
        {
            bool istrue = false ;
            hf.Model.CaseDelegate cd = DelegateController.getController().getCaseDelegateModel(int.Parse(DelegateID));
            if (this.getLoginAccount() != null)
            {
                if (cd.UserID == this.getLoginAccount().ID)
                {
                    istrue = true;
                }
                else {
                    istrue = false;
                }
            }
            else {
                if (this.getLoginLawyer() != null)
                {
                    istrue = true;
                }
                else {
                    istrue = false;
                }
            }
            return istrue;
        }

        /// <summary>
        /// 得到法规状态
        /// </summary>
        /// <param name="Status"></param>
        /// <returns></returns>
        public string GetLegislationStatus(string Status)
        {
            string outstr = "";
            switch (Status)
            { 
                case "1":
                    outstr = "有效";
                    break;
                case "2":
                    outstr = "失效";
                    break;
                case "3":
                    outstr = "被修订";
                    break;
                case "4":
                    outstr = "被修正";
                    break;
            }
            return outstr;
        }
    }
}
